package com.silence.gankio.fragment


import android.Manifest
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.View
import com.silence.gankio.BaseFragment
import com.silence.gankio.R
import com.silence.gankio.notifieation.TestNotification
import com.silence.gankio.util.PermissionPageUtils
import com.silence.gankio.util.ToastUtil
import com.silence.gankio.widget.LabelView
import com.tbruyelle.rxpermissions2.RxPermissions
import com.xuexiang.xui.widget.dialog.strategy.impl.MaterialDialogStrategy
import kotlinx.android.synthetic.main.fragment_main.*

/**
 * A simple [Fragment] subclass.
 *
 */
class MainFragment : Fragment(), View.OnClickListener {

//    override fun getFragmentContentView() = R.layout.fragment_main
    private var number = 0
    private val lableContent = mutableListOf<String>()
    private var addLabel: View? = null

    private lateinit var mRxPermissions: RxPermissions

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mRxPermissions = RxPermissions(this)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        label_view.setOnAddClickListener(object : LabelView.OnLabelViewListener {
            override fun onAddLabelClicked(view: View) {
                addLabel = view
                val content = "标签$number"
                label_view.addLabel(content)
                lableContent.add(content)
                number++
                if (label_view.childCount > 3) {
                    addLabel?.visibility = View.GONE
                }
            }

            override fun onLabelClicked(view: View, index: Int, content: String) {
                lableContent -= content
                label_view.removeView(view)
                if (label_view.childCount <= 3) {
                    addLabel?.visibility = View.VISIBLE
                }
            }
        })

        btn.setOnClickListener {
            ToastUtil.showToast(context, lableContent.toString())
        }

        btn_1.setOnClickListener(this)
        btn_2.setOnClickListener(this)
        btn_3.setOnClickListener(this)
        btn_4.setOnClickListener(this)


//        tv.setOnClickListener {
//            flex_box.addText("我是标签$number")
//            number++
//        }
//
//        flex_box.setOnLabelFlexLayoutListener(object : LabelFlexLayout.OnLabelFlexLayoutListener{
//            override fun onAddChild(view: View, text: String) {
//
//            }
//        })

    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.btn_1 -> {
                mRxPermissions.request(Manifest.permission.CAMERA)
                    .subscribe { t ->
                        if (t) {
                            ToastUtil.showToast(context, "已获取相机权限")
                            TestNotification.notify(context!!, "哈哈哈", number)
                        } else {
                            ToastUtil.showToast(context, "相机权限被拒绝")
                        }
                    }
            }
            R.id.btn_2 -> {
                mRxPermissions.request(Manifest.permission.READ_PHONE_NUMBERS)
                    .subscribe { t ->
                        if (t) {
                            ToastUtil.showToast(context, "已获取读取手机号码")
                        } else {
                            MaterialDialogStrategy().showConfirmDialog(context!!, "跳转设置", "确定",
                                { _, _ ->
                                    PermissionPageUtils(context!!).jumpPermissionPage()
                                }, "取消"
                            ).show()
                        }
                    }
            }
            R.id.btn_3 -> {
                mRxPermissions.request(Manifest.permission.READ_PHONE_STATE)
                    .subscribe { t ->
                        if (t) {
                            ToastUtil.showToast(context, "已获取手机状态权限")
                        } else {
                            ToastUtil.showToast(context, "手机状态权限被拒绝")
                        }
                    }
            }
            R.id.btn_4 -> {
                mRxPermissions.request(Manifest.permission.CAMERA)
                    .subscribe { t ->
                        if (t) {
                            ToastUtil.showToast(context, "已获取相机权限")
                        } else {
                            ToastUtil.showToast(context, "相机权限被拒绝")
                        }
                    }
            }
            else -> {
            }
        }
    }
}
